﻿/*************************************************************************
 *  Copyright (C), 2016-2017, Mogoson tech. Co., Ltd.
 *  FileName: TerrainExtension.cs
 *  Author: Mogoson   Version: 1.0   Date: 4/29/2016
 *  Version Description:
 *    Internal develop version,mainly to achieve its function.
 *  File Description:
 *    Ignore.
 *  Class List:
 *    <ID>           <name>             <description>
 *     1.         TerrainExtension             Ignore.
 *  Function List:
 *    <class ID>     <name>             <description>
 *     1.
 *  History:
 *    <ID>    <author>      <time>      <version>      <description>
 *     1.     Mogoson     4/29/2016       1.0        Build this file.
 *************************************************************************/

namespace Developer.Terrain
{
    using UnityEngine;

    /// <summary>
    /// Extension of terrain.
    /// </summary>
    public static class TerrainExtension
    {
        #region Public Method
        /// <summary>
        /// Get normalized position relative to terrain.
        /// </summary>
        /// <param name="terrain">Target terrain.</param>
        /// <param name="woldPos">Target position in wold.</param>
        /// <returns>Normalized position.</returns>
        public static Vector3 GetNormalizedPositionRelativeToTerrain(this Terrain terrain, Vector3 woldPos)
        {
            var dSize = terrain.terrainData.size;
            var coord = woldPos - terrain.transform.position;
            return new Vector3(coord.x / dSize.x, coord.y / dSize.y, coord.z / dSize.z);
        }//GetNormal...()_end

        /// <summary>
        /// Get relative terrain position from map.
        /// </summary>
        /// <param name="terrain">Base terrain.</param>
        /// <param name="mapSize">Map size(x is width, z is height).</param>
        /// <param name="woldPos">Target position in wold.</param>
        /// <returns>Relative position.</returns>
        public static Vector3 GetRelativeTerrainPositionFromMap(this Terrain terrain, Vector3 mapSize, Vector3 woldPos)
        {
            Vector3 coord = GetNormalizedPositionRelativeToTerrain(terrain, woldPos);
            return new Vector3(coord.x * mapSize.x, 0, coord.z * mapSize.z);
        }//GetRelative...()_end
        #endregion
    }//class_end
}//namespace_end